package com.lxblyw.service;

import java.util.ArrayList;
import java.util.List;

import com.lxblyw.dao.DocumentDao;
import com.lxblyw.dao.EmployeeDao;
import com.lxblyw.dao.ProjectDao;
import com.lxblyw.entity.Document;
import com.lxblyw.entity.DocumentItem;
import com.lxblyw.entity.Employee;
import com.lxblyw.entity.Project;
import com.lxblyw.util.DateUtil;
import com.lxblyw.util.PKUtil;

public class DocumentService {
	
	private EmployeeDao emDao = new EmployeeDao();
	private ProjectDao projDao = new ProjectDao();
	private DocumentDao docDao = new DocumentDao();
	public List<DocumentItem> list(String id) {
		List<Document> docList = docDao.findAll(id);
		List<DocumentItem> docItemList = new ArrayList<DocumentItem>();
		for(Document doc : docList) {
			Employee employee = emDao.findById(doc.getEmployeeid());
			Project project = projDao.findProjectById(doc.getProjectid());
			
			DocumentItem docItem = new DocumentItem();
			docItem.setDoc(doc);
			docItem.setEmployee(employee);
			docItem.setProject(project);
			
			docItemList.add(docItem);
		}
		
		
		return docItemList;
	}
	public void save(String title, String content, String employeeid, String projectid) {
		Document doc = new Document();
		doc.setId(PKUtil.getPK());
		doc.setTitle(title);
		doc.setContent(content);
		doc.setCreatetime(DateUtil.getNow());
		doc.setEmployeeid(employeeid);
		doc.setProjectid(projectid);
		
		docDao.save(doc);
		
	}
	public void del(String id) {
		docDao.del(id);
		
	}
	public void update(String id, String title, String content) {
		docDao.update(id,title,content);
		
	}
	public Document findById(String id) {
		return docDao.findById(id);
	}
	
	
}
